package org.jeecg.modules.demo.ch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.demo.ch.Utils.OrderBomUtils;
import org.jeecg.modules.demo.ch.common.CommonMst;
import org.jeecg.modules.demo.ch.entity.ChDesignBomDetail;
import org.jeecg.modules.demo.ch.entity.ChDesignBomExcel;
import org.jeecg.modules.demo.ch.entity.ChDesignBomMaster;
import org.jeecg.modules.demo.ch.entity.ChOrderBom;
import org.jeecg.modules.demo.ch.mapper.ChDesignBomExcelMapper;
import org.jeecg.modules.demo.ch.service.IChDesignBomDetailService;
import org.jeecg.modules.demo.ch.service.IChDesignBomExcelService;
import org.jeecg.modules.demo.ch.service.IChDesignBomMasterService;
import org.jeecg.modules.demo.ch.service.IChOrderBomService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description: 模具设计BOM导入EXCEL表
 * @Author: jeecg-boot
 * @Date:   2022-09-23
 * @Version: V1.0
 */
@Service
public class ChDesignBomExcelServiceImpl extends ServiceImpl<ChDesignBomExcelMapper, ChDesignBomExcel> implements IChDesignBomExcelService {

    @Autowired
    private IChDesignBomDetailService iChDesignBomDetailService; //设计BOM

    @Autowired
    private IChOrderBomService iChOrderBomService; //设计BOM

    @Autowired
    private IChDesignBomMasterService iChDesignBomMasterService; //模具设计BOM主表


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchImportExcel(List<ChDesignBomExcel> chDesignBomExcelList) {
        String chDesignBomMasterId = chDesignBomExcelList.get(0).getChDesignBomMasterId();//模具设计BOM主表id
        //根据模具设计BOM主表id 获取模具编码
        LambdaQueryWrapper<ChDesignBomMaster> chDesignBomMasterLambdaQueryWrapper = new QueryWrapper<ChDesignBomMaster>().lambda();
        chDesignBomMasterLambdaQueryWrapper.eq(ChDesignBomMaster::getId,chDesignBomMasterId);
        ChDesignBomMaster chDesignBomMaster = iChDesignBomMasterService.getOne(chDesignBomMasterLambdaQueryWrapper);
        if (null == chDesignBomMaster){
            throw new RuntimeException("根据模具设计BOM主表id未找到模具设计BOM主表数据");
        }
        String moldCode = chDesignBomMaster.getMoldCode();//模具编码


        //根据模号(模具设计BOM主表id)和零件名查询是否为空  不为空忽略  为空 进行新增
        //根据(模具设计BOM主表id) 获取上一版本的id  为空 直接新增版本为1
        QueryWrapper<ChDesignBomDetail> chDesignBomDetailQueryWrapper = new QueryWrapper<>();
        chDesignBomDetailQueryWrapper.select("MAX(version) as version");
        chDesignBomDetailQueryWrapper.eq("ch_design_bom_master_id",chDesignBomMasterId);
        ChDesignBomDetail one = iChDesignBomDetailService.getOne(chDesignBomDetailQueryWrapper);

        for (ChDesignBomExcel chDesignBomExcel : chDesignBomExcelList) {

            ChDesignBomDetail chDesignBomDetail = new ChDesignBomDetail(); //设计BOM
            BeanUtils.copyProperties(chDesignBomExcel, chDesignBomDetail);
            chDesignBomDetail.setSerialNumber(OrderBomUtils.setIntegerIfNotNull(chDesignBomExcel.getSerialNumber()));//类型不一致 单独复制
            chDesignBomDetail.setCreateTime(new Date());

            ChOrderBom orderBom = new ChOrderBom(); //订购BOM (更新设计BOM数据)
            BeanUtils.copyProperties(chDesignBomExcel, orderBom);
            orderBom.setT100MoldNo(moldCode);//订购BOM 赋值模具编码初始值
            orderBom.setDesignSerialNo(OrderBomUtils.setStringIfNotNull(chDesignBomExcel.getSerialNumber()));
            orderBom.setSerialNumber(OrderBomUtils.setIntegerIfNotNull(chDesignBomExcel.getSerialNumber()));
            orderBom.setCreateTime(new Date());


            if (one == null) {//新增  设计BOM  订购BOM

                chDesignBomDetail.setVersion(CommonMst.ORDER_BOM.INIT_VERSION);
                orderBom.setVersion(CommonMst.ORDER_BOM.INIT_VERSION);
            } else {//不为空   bom状态（1.新增:蓝色2.变更：红色3.不变：黑色 4.取消：紫色）

                // 根据(模具设计BOM主表id) 获取上一版本的id  复制上一版的数据  进行版本+1 新增
                String newVersion = String.valueOf(Integer.parseInt(one.getVersion()) + 1);
                chDesignBomDetail.setVersion(newVersion);
                orderBom.setVersion(newVersion);
                LambdaQueryWrapper<ChDesignBomDetail> chDesignBomDetailLambdaQueryWrapper1 = new QueryWrapper<ChDesignBomDetail>().lambda();
                //根据模具编号(模具设计BOM主表id) 和 零件名判断是否原来数据存在
                chDesignBomDetailLambdaQueryWrapper1.eq(ChDesignBomDetail::getChDesignBomMasterId, chDesignBomMasterId);
                chDesignBomDetailLambdaQueryWrapper1.eq(ChDesignBomDetail::getPartName, chDesignBomExcel.getPartName());
                List<ChDesignBomDetail> chDesignBomDetailList = iChDesignBomDetailService.list(chDesignBomDetailLambdaQueryWrapper1);

                if (CollectionUtils.isEmpty(chDesignBomDetailList)) {//为空   状态为新增
                    chDesignBomDetail.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_ADD);//1.新增
                    orderBom.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_ADD);
                } else {//不为空
                    chDesignBomDetail.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UNCHANGED);//3.不变：黑色
                    orderBom.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UNCHANGED);
                }
            }
            //新增设计BOM 和 订购BOM
            if (!chDesignBomDetail.getVersion().equals(CommonMst.ORDER_BOM.INIT_VERSION)){
                chDesignBomDetail.setId(""); //只有版本为1的时候 直接用原来的id
            }
            if (!orderBom.getVersion().equals(CommonMst.ORDER_BOM.INIT_VERSION)){
                orderBom.setId("");//只有版本为1的时候 直接用原来的id
            }
            iChDesignBomDetailService.save(chDesignBomDetail);
            iChOrderBomService.save(orderBom);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importAllExcel(List<ChDesignBomExcel> chDesignBomExcelList) {
        String chDesignBomMasterId = chDesignBomExcelList.get(0).getChDesignBomMasterId();//模具设计BOM主表id
        //根据模具设计BOM主表id 获取模具编码
        LambdaQueryWrapper<ChDesignBomMaster> chDesignBomMasterLambdaQueryWrapper = new QueryWrapper<ChDesignBomMaster>().lambda();
        chDesignBomMasterLambdaQueryWrapper.eq(ChDesignBomMaster::getId,chDesignBomMasterId);
        ChDesignBomMaster chDesignBomMaster = iChDesignBomMasterService.getOne(chDesignBomMasterLambdaQueryWrapper);
        if (null == chDesignBomMaster){
            throw new RuntimeException("根据模具设计BOM主表id未找到模具设计BOM主表数据");
        }
        String moldCode = chDesignBomMaster.getMoldCode();//模具编码


        //根据(模具设计BOM主表id) 获取上一版本的id  为空 直接新增版本为1
        QueryWrapper<ChDesignBomDetail> chDesignBomDetailQueryWrapper = new QueryWrapper<>();
        chDesignBomDetailQueryWrapper.select("MAX(version) as version");
        chDesignBomDetailQueryWrapper.eq("ch_design_bom_master_id",chDesignBomMasterId);
        ChDesignBomDetail one = iChDesignBomDetailService.getOne(chDesignBomDetailQueryWrapper);


        //根据模号(模具设计BOM主表id)和零件名查询是否为空  不为空忽略  为空 进行新增
        for (ChDesignBomExcel chDesignBomExcel : chDesignBomExcelList) {

            ChDesignBomDetail chDesignBomDetail = new ChDesignBomDetail(); //设计BOM
            BeanUtils.copyProperties(chDesignBomExcel, chDesignBomDetail);
            chDesignBomDetail.setSerialNumber(OrderBomUtils.setIntegerIfNotNull(chDesignBomExcel.getSerialNumber()));//类型不一致 单独复制
            chDesignBomDetail.setCreateTime(new Date());

            ChOrderBom orderBom = new ChOrderBom(); //订购BOM (更新设计BOM数据)
            BeanUtils.copyProperties(chDesignBomExcel, orderBom);
            orderBom.setT100MoldNo(moldCode);//订购BOM 赋值模具编码初始值
            orderBom.setDesignSerialNo(OrderBomUtils.setStringIfNotNull(chDesignBomExcel.getSerialNumber()));
            orderBom.setSerialNumber(OrderBomUtils.setIntegerIfNotNull(chDesignBomExcel.getSerialNumber()));
            orderBom.setCreateTime(new Date());

            if (one == null) {//新增  设计BOM  订购BOM

                chDesignBomDetail.setVersion(CommonMst.ORDER_BOM.INIT_VERSION);
                orderBom.setVersion(CommonMst.ORDER_BOM.INIT_VERSION);
            } else {//不为空   bom状态（1.新增:蓝色2.变更：红色3.不变：黑色 4.取消：紫色）

                // 根据(模具设计BOM主表id) 获取上一版本的id  复制上一版的数据  进行版本+1 新增
                String newVersion = String.valueOf(Integer.parseInt(one.getVersion()) + 1);
                chDesignBomDetail.setVersion(newVersion);
                orderBom.setVersion(newVersion);

                //判断零件名是否为空
                String partName = chDesignBomExcel.getPartName();//零件名
                if (StringUtils.hasLength(partName)) {//零件名不为空
                    //根据模具编号(模具设计BOM主表id) 零件名判断是否原来数据存在
                    LambdaQueryWrapper<ChDesignBomDetail> chDesignBomDetailLambdaQueryWrapper1 = new QueryWrapper<ChDesignBomDetail>().lambda();
                    chDesignBomDetailLambdaQueryWrapper1.eq(ChDesignBomDetail::getChDesignBomMasterId, chDesignBomMasterId);
                    chDesignBomDetailLambdaQueryWrapper1.eq(ChDesignBomDetail::getPartName, chDesignBomExcel.getPartName());
                    List<ChDesignBomDetail> chDesignBomDetailList1 = iChDesignBomDetailService.list(chDesignBomDetailLambdaQueryWrapper1);
                    if (CollectionUtils.isEmpty(chDesignBomDetailList1)) {//为空
                        chDesignBomDetail.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_ADD);//1.新增
                        orderBom.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_ADD);
                    } else {//不为空
                        LambdaQueryWrapper<ChDesignBomDetail> chDesignBomDetailLambdaQueryWrapper2 = new QueryWrapper<ChDesignBomDetail>().lambda();
                        //根据模具编号(模具设计BOM主表id) 零件名 、 中文名、英文名、零件规格、材料、数量、备注   判断是否原来数据存在
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getChDesignBomMasterId, chDesignBomMasterId);
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getPartName, chDesignBomExcel.getPartName());
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getChineseName, chDesignBomExcel.getChineseName());
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getEnglishName, chDesignBomExcel.getEnglishName());
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getPartSpecification, chDesignBomExcel.getPartSpecification());
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getMaterial, chDesignBomExcel.getMaterial());
                        List<ChDesignBomDetail> chDesignBomDetailList2 = iChDesignBomDetailService.list(chDesignBomDetailLambdaQueryWrapper2);
                        if (CollectionUtils.isEmpty(chDesignBomDetailList2)) {//为空
                            chDesignBomDetail.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UPDATE);//变更
                            orderBom.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UPDATE);
                        } else {//不为空
                            chDesignBomDetail.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UNCHANGED);//不变
                            orderBom.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UNCHANGED);
                        }
                    }
                } else {//零件名为空
                    LambdaQueryWrapper<ChDesignBomDetail> chDesignBomDetailLambdaQueryWrapper1 = new QueryWrapper<ChDesignBomDetail>().lambda();
                    //根据模具编号(模具设计BOM主表id) 和 中文名、英文名、规格  判断是否原来数据存在
                    chDesignBomDetailLambdaQueryWrapper1.eq(ChDesignBomDetail::getChDesignBomMasterId, chDesignBomMasterId);
                    chDesignBomDetailLambdaQueryWrapper1.eq(ChDesignBomDetail::getChineseName, chDesignBomExcel.getChineseName());
                    chDesignBomDetailLambdaQueryWrapper1.eq(ChDesignBomDetail::getEnglishName, chDesignBomExcel.getEnglishName());
                    chDesignBomDetailLambdaQueryWrapper1.eq(ChDesignBomDetail::getPartSpecification, chDesignBomExcel.getPartSpecification());
                    List<ChDesignBomDetail> chDesignBomDetailList1 = iChDesignBomDetailService.list(chDesignBomDetailLambdaQueryWrapper1);

                    if (CollectionUtils.isEmpty(chDesignBomDetailList1)) {//为空   状态为新增
                        chDesignBomDetail.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_ADD);//1.新增
                        orderBom.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_ADD);
                    } else {//不为空
                        //精确匹配数量、材料、备注栏位
                        LambdaQueryWrapper<ChDesignBomDetail> chDesignBomDetailLambdaQueryWrapper2 = new QueryWrapper<ChDesignBomDetail>().lambda();
                        //根据模具编号(模具设计BOM主表id) 和 中文名、英文名、规格  判断是否原来数据存在
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getChDesignBomMasterId, chDesignBomMasterId);
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getChineseName, chDesignBomExcel.getChineseName());
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getEnglishName, chDesignBomExcel.getEnglishName());
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getPartSpecification, chDesignBomExcel.getPartSpecification());
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getNumber, chDesignBomExcel.getNumber());
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getMaterial, chDesignBomExcel.getMaterial());
                        chDesignBomDetailLambdaQueryWrapper2.eq(ChDesignBomDetail::getRemark, chDesignBomExcel.getRemark());
                        List<ChDesignBomDetail> chDesignBomDetailList2 = iChDesignBomDetailService.list(chDesignBomDetailLambdaQueryWrapper2);
                        if (CollectionUtils.isEmpty(chDesignBomDetailList2)) {//为空
                            chDesignBomDetail.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UPDATE);//变更
                            orderBom.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UPDATE);
                        } else {//不为空
                            chDesignBomDetail.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UNCHANGED);//3.不变：黑色
                            orderBom.setBomStatus(CommonMst.ORDER_BOM.BOM_STATUS_UNCHANGED);
                        }
                    }
                }
            }
            //新增设计BOM 和 订购BOM
            if (!chDesignBomDetail.getVersion().equals(CommonMst.ORDER_BOM.INIT_VERSION)){
                chDesignBomDetail.setId(""); //只有版本为1的时候 直接用原来的id
            }
            if (!orderBom.getVersion().equals(CommonMst.ORDER_BOM.INIT_VERSION)){
                orderBom.setId("");//只有版本为1的时候 直接用原来的id
            }
            //新增设计BOM 和 订购BOM
            iChDesignBomDetailService.save(chDesignBomDetail);
            iChOrderBomService.save(orderBom);
        }
    }
}
